package com.gzist.zzchatroomserver.controller.admin;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gzist.zzchatroomserver.common.Result;
import com.gzist.zzchatroomserver.entity.dto.ResetPasswordDTO;
import com.gzist.zzchatroomserver.entity.dto.UserDTO;
import com.gzist.zzchatroomserver.entity.dto.UserSaveDTO;
import com.gzist.zzchatroomserver.entity.po.User;
import com.gzist.zzchatroomserver.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * 用户表
 *
 * @author hzj
 * @since 2024-10-30
 */
@RestController
@Api(tags ="用户管理")
@RequestMapping("/admin/user")
@RequiredArgsConstructor
public class UserAdminController {

    private final UserService userService;

    /**
     * 用户登录
     * @param userDTO
     * @return
     */
    @PostMapping("/login")
    @ApiOperation(value = "用户登录",notes = "User")
    public Result<SaTokenInfo> login(@RequestBody @ApiParam("登录参数") UserDTO userDTO){
        userService.login(userDTO);
        return Result.success(StpUtil.getTokenInfo(),"登录成功");
    }

    @PostMapping("/page")
    @ApiOperation(value = "用户分页列表查询",notes = "page")
    public Result<Page<User>> page(@RequestParam(value = "页码", defaultValue = "1") Integer pageNum
            , @RequestParam(value = "条数", defaultValue = "5") Integer pageSize){
        return Result.success(userService.page(new Page<>(pageNum,pageSize)),"查询分页列表成功");
    }

    @GetMapping("/detail/{id}")
    @ApiOperation(value = "用户查看详情",notes = "User")
    public Result<User> getUserById(@PathVariable("id") @ApiParam("用户id") String id){
        return Result.success(userService.getById(id),"用户信息查询成功");
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除用户",notes = "User")
    public Result remove(@PathVariable("id") @ApiParam("用户id") String id){userService.removeById(id);
        return Result.success("删除成功");
    }
    @PostMapping("/deletes")
    @ApiOperation(value = "批量删除用户",notes = "User")
    public Result removes(@RequestBody @ApiParam("用户id集合") List<String> ids){
        userService.removeByIds(ids);
        return Result.success("删除成功");
    }

    @PostMapping("/saveOrUpdate")
    @ApiOperation(value = "添加或者编辑用户",notes = "User")
    public Result saveOrUpdate(@RequestBody @ApiParam("用户信息") UserSaveDTO userSaveDTO){
        userService.saveOrUpdateUser(userSaveDTO);
        return Result.success("添加成功");
    }
    @PostMapping("/updatePassword")
    @ApiOperation("修改密码")
    public Result updatePassword(@RequestBody @ApiParam("输入的密码集合") HashMap<String,String> passwordMap){
        userService.updatePassword(passwordMap);
        return Result.success("修改密码成功");
    }

    /**
     * 重置密码
     * 无需鉴权，方便用户忘记密码时可以随时进行重置
     * @param resetPasswordDTO 用户id和新密码
     */
    @PostMapping("/resetPassword")
    @ApiOperation(value = "重置密码",notes = "ResetPasswordDTO")
    public Result resetPassword(@RequestBody @ApiParam("用户id和新密码") ResetPasswordDTO resetPasswordDTO){
        userService.resetPassword(resetPasswordDTO);
        return Result.success("重置成功");
    }
}
